package mumu.android.lib.view.webview;


import mumu.android.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MWebChromeClient extends WebChromeClient {

	private final String TAG="dd";
	private Handler handler;

	public MWebChromeClient(IWebChromeClient iWebChromeClient) {
		//this.handler = handler;
		this.iWebChromeClient=iWebChromeClient;
	}

	/*
	 * 这种加载更新方式，思路比较清晰明了 (non-Javadoc) // 设置 加载进程
	 * 
	 * @see
	 * android.webkit.WebChromeClient#onProgressChanged(android.webkit.WebView ,
	 * int)
	 */
	public void onProgressChanged(WebView view, int progress) {
		super.onProgressChanged(view, progress);
		if (iWebChromeClient != null) {
			iWebChromeClient.onProgressChanged(view, progress);
		}
		
	}

	@Override
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		Log.d(TAG, message);
		if (!TextUtils.isEmpty(message)) {
			Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
		}
		result.confirm();
		return true;

	}

	@Override
	public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
		new AlertDialog.Builder(view.getContext())
		// .setTitle(R.string.title_dialog_confirm)
				.setMessage(message).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						result.confirm();
					}
				}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						result.cancel();
					}
				}).create().show();
		return true;
	}

	@Override
	public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
		final LayoutInflater factory = LayoutInflater.from(view.getContext());
		final View v = factory.inflate(R.layout.javascript_propt_dialog, null);

		((TextView) v.findViewById(R.id.prompt_message_text)).setText(message);
		((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue);

		new AlertDialog.Builder(view.getContext())
		// .setTitle(R.string.title_dialog_prompt)
				.setView(v).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString();
						result.confirm(value);
					}
				}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						result.cancel();
					}
				}).setOnCancelListener(new DialogInterface.OnCancelListener() {
					public void onCancel(DialogInterface dialog) {
						result.cancel();
					}
				}).show();

		return true;
	}

	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);
	};

	private IWebChromeClient iWebChromeClient;

	public void setIWebChromeClient(IWebChromeClient iWebChromeClient) {
		this.iWebChromeClient = iWebChromeClient;
	}

	public interface IWebChromeClient {
		void onProgressChanged(WebView view, int progress);
	}
}
